B.WebBase = function () {
    var _ = this;

    _.l = function () {
        return (new B.Web((new Date).getTime() + "Web", _.Name, _.Link, _.Note, _.Cate, _.Cr, _.Key, _.Lans, _.tagName, _.Parent)).c();
    };

    _.a = function (p) {
        var _ = this;
        var t = "{'action':'AddWeb','data':{'table':{'ID':'" + _.Parent.Parent.ID + "','row':{'ID':'" + _.Parent.ID + "','web':{'ID':'" + _.ID + "','Name':'" + _.Name + "','Link':'" + _.Link + "','Note':'" + _.Note + "'},'index':'" + p + "'}}}}";
        g.ComWithServer.add(g.domainDirectory + "/ResAddApp.aspx?paraStr=" + paraStr +"&flag=" + g.subdir, t);
        g.ComWithServer.startSend(); //将信息发往服务器.
        g.tip.display(lan["02"]);
    };

    _.b = function (p, q) {
        var _ = this;
        var t = "{'action':'CopyWeb','data':{'from': {'table':{'ID':'" + _.Parent.Parent.ID + "','row':{'ID':'" + _.Parent.ID + "','web':{'ID':'" + _.ID + "'}}}},'to':{'table':{'ID':'" + p.Parent.Parent.ID + "','row':{'ID':'" + p.Parent.ID + "','web':{'ID':'" + p.ID + "'}}}},'index':'" + q + "'}}";
        g.ComWithServer.add(g.domainDirectory + "/ResAddApp.aspx?paraStr=" + paraStr +"&flag=" + g.subdir, t);
        g.ComWithServer.startSend(); //将信息发往服务器.
        g.tip.display(lan["02"]);
    };

    _.f = function () {
        var _ = this;
        var t = "{'action':'DeleteWeb','data':{'table':{'ID':'" + _.Parent.Parent.ID + "','row':{'ID':'" + _.Parent.ID + "','web':{'ID':'" + _.ID + "'}}}}";
        g.ComWithServer.add(g.domainDirectory + "/ResAddApp.aspx?paraStr=" + paraStr +"&flag=" + g.subdir, t);
        g.ComWithServer.startSend(); //将信息发往服务器.
        g.tip.display(lan["02"]);
    };

    _.u = function () {
        var _ = this;
        var t = "{'action':'UpdateWeb','data':{'table':{'ID':'" + _.Parent.Parent.ID + "','row':{'ID':'" + _.Parent.ID + "','web':{'ID':'" + _.ID + "','Name':'" + _.Name + "','Link':'" + _.Link + "','Note':'" + _.Note + "'}}}}}";
        g.ComWithServer.add(g.domainDirectory + "/ResAddApp.aspx?paraStr=" + paraStr +"&flag=" + g.subdir, t);
        g.ComWithServer.startSend(); //将信息发往服务器.
        g.tip.display(lan["02"]);
    };

    _.v = function (_webRow, _index) {
        var _ = this;
        var t = "{'action':'MoveWeb','data':{'from': {'table':{'ID':'" + _.Parent.Parent.ID + "','row':{'ID':'" + _.Parent.ID + "','web':{'ID':'" + _.ID + "'}}}},'to':{'table':{'ID':'" + _webRow.Parent.ID + "','row':{'ID':'" + _webRow.ID + "'}}},'index':'" + _index + "'}}";
        g.ComWithServer.add(g.domainDirectory + "/ResAddApp.aspx?paraStr=" + paraStr +"&flag=" + g.subdir, t);
        g.ComWithServer.startSend(); //将信息发往服务器.
        g.tip.display(lan["02"]);
    };

    _.g = function () { //deleteSelf
        var _ = this;
        var t = _.Parent;
        lib.dom.setStyle(t.delButton, "cursor:default;visibility:hidden;");
        for (var i = 0; i < t.Webs.length; i++) {
            if (t.Webs[i].ID == _.ID) {
                if (t.Webs.length - 1 == t.numDisFirst) { //如果当前是最后一个，则要防止删除后不显示任何Web
                    var preWeb = t.getFWebFromPreRow(_);
                    t.numDisFirst = lib.data.getOrder(t.Webs, preWeb);

                    var a = "{'action':'numDisFirst','data':{'table':{'ID':'" + t.Parent.ID + "','row':{'ID':'" + t.ID + "','numDisFirst':'" + t.numDisFirst + "'}}}}";
                    g.ComWithServer.add(g.domainDirectory + "/ResAddApp.aspx?paraStr=" + paraStr +"&flag=" + g.subdir, a);
                    g.ComWithServer.startSend(); //将信息发往服务器.
                }
                t.Webs.splice(i, 1);
                t.d();
                t.refresh();
                _.f();
                //console.log(g.dataWebTable.main.page.Mtables.table, t.Parent.ID, t.ID);
                g.dataWebTable.main.page.Mtables.table
                    .find(x => x.Id == t.Parent.ID).Mrows.row
                    .find(x => x.Id == t.ID).Mrowwebs.web.splice(i, 1);
                break;
            }
        }
    };

    _.moveWebItem = function (_webRow, _index) {
        var _ = this;
        for (var i = 0; i < _.Parent.Webs.length; i++) {
            if (_.Parent.Webs[i].ID == _.ID) {

                if (_.Parent.Webs.length - 1 == _.Parent.numDisFirst) { //如果当前是最后一个，则要防止移除后不显示任何Web
                    var preWeb = _.Parent.getFWebFromPreRow(_);
                    _.Parent.numDisFirst = lib.data.getOrder(_.Parent.Webs, preWeb);

                    var a = "{'action':'numDisFirst','data':{'table':{'ID':'" + _.Parent.Parent.ID + "','row':{'ID':'" + _.Parent.ID + "','numDisFirst':'" + _.Parent.numDisFirst + "'}}}}";
                    g.ComWithServer.add(g.domainDirectory + "/ResAddApp.aspx?paraStr=" + paraStr +"&flag=" + g.subdir, a);
                    g.ComWithServer.startSend(); //将信息发往服务器.
                }

                _.Parent.Webs.splice(i, 1);
                _.v(_webRow, _index);
                _.Parent.d(); //d()会改变当前web的parent,所以.v()要放在前面.
                _.Parent.refresh();
                break;
            }
        }
        _webRow.Webs.splice(_index, 0, _);
        _webRow.d();
        _webRow.refresh();

    };

    _.h = function (p) { //updateSelf
        var _ = this;
        _.Name = p.Name;
        _.Link = p.Link;
        _.href = p.Link;
        _.Note = p.Note;
        _.Cate = p.Cate; //category.
        _.Cr = p.Cr; //creator
        _.Key = p.Key;
        _.Lans = p.Lans; //languages
        _.E.innerHTML = p.Name;
        $SA(_.E, "Link", p.Link); //一定要使用_this,而不是this, 否则this所引用的对象会不是期望的.
        _.E.title = p.Note;
        _.Parent.refresh();
        _.u(); //发送到服务器
    };

    _.i = function () {
        var _ = this;
        _.E.innerHTML = _.Name;
        $SA(_.E, "Link", _.Link); //一定要使用_this,而不是this, 否则this所引用的对象会不是期望的.
        $SA(_.E, "Note", _.Note);
        $SA(_.E, "Cate", _.Cate);
        $SA(_.E, "Cr", _.Cr);
    };

    _.j = function (p) {
        var _ = this;
        var t = {};
        if (p) {
            t.ID = lib.Time.getID() + "WEB";
        } else {
            t.ID = _.ID;
        }
        t.Name = _.Name;
        t.Link = _.Link;
        t.Note = _.Note;
        t.Cr = _.Cr;
        return t;
    };

    _.k = function () {
        var _ = this;
        if ($("deleteWeb" + _.ID))
            return;
        _.ShowDeleteEle = $CE("DIV");
        var tmpCsstext = "display:inline;background-color:#DDEEDD;borderStyle:solid;borderWidth:1px;borderColor:#5858FA;float:left;";
        lib.dom.setStyle(_.ShowDeleteEle, tmpCsstext);
        $SA(_.ShowDeleteEle, "id", "deleteWeb" + _.ID);
        _.ShowDeleteEle.innerHTML = "<span style='cursor:default;' relationalID='" + _.Parent.Parent.ID + "-" + _.Parent.ID + "-" + _.ID + "' onclick='deleteWeb'>x</span>";
        addEleAround(_.E, _.ShowDeleteEle, "topright");
    };

    _.m = function () {
        var _ = this;
        _.E.style.backgroundColor = "#B0E0E6";
    };

    _.n = function () {
        var _ = this;
        _.E.style.backgroundColor = "#DDEEDD";
    };
};

B.WebRowBase = function () {
    var _ = this;

    _.getFWebFromPreRow = function (p) { //*p*   Type: Web    //获取前一行的第一个Web
        var _ = this;
        var a = lib.data.getOrder(_.Webs, p);
        var b = 0;
        for (var i = a; i >= 0; i--) {
            b = b + lib.data.getStrLength(_.Webs[i].Name) * 8 + 10; //从后往前累加字符串长度.
            if (b > g.mainTableWidth) { //长度达到g.mainTableWidth
                return _.Webs[i]; //
                break;
            }
        }
        return _.Webs[0]; //如果到头了,返回首个Web
    };

    _.getFWebFromNexRow = function (p) { //*p*   Type: Web  获取下一行的第一个Web
        var _ = this;
        var a = lib.data.getOrder(_.Webs, p);
        var b = 0;
        for (var i = a + 1; i < _.Webs.lenth; i++) {
            b = b + lib.data.getStrLength(_.Webs[i].Name) * 8 + 10; //从后往前累加字符串长度.
            if (b > g.mainTableWidth) { //长度达到g.mainTableWidth
                return _.Webs[i]; //
                break;
            }
        }
        return _.Webs[_.Webs.lenth - 1]; //如果到头了,返回最后一个Web
    };

    _.closeEditor = function () {
        var _ = this;
        _.Webs.forEach(function (_p) {
            _p.closeEditor();
        });
    };

    _.l = function () { //深克隆，要改变当前ID和子元素的ID
        var _ = this;
        var t = new WebRow((new Date).getTime() + "CATE", _.CateName, _.parent, _.webIDHTML, _.numDisFirst);
        for (var i = 0; i < _.Webs.length; i++) {
            t.Webs.push(_.Webs[i].l());
        }
        return t;
    };

    _.a = function (p) {
        var _ = this;
        var t = "{'action':'AddRow','data':{'table':{'ID':'" + _.Parent.ID + "','row':{'ID':'" + _.ID + "','CateName':'" + _.CateName + "'},'index':'" + p + "'}}";
        g.ComWithServer.add(g.domainDirectory + "/ResAddApp.aspx?paraStr=" + paraStr +"&flag=" + g.subdir, t);
        g.ComWithServer.startSend(); //将信息发往服务器.
        g.tip.display(lan["02"]);
    }

    _.b = function (p, q) { //在服务端深克隆一个元素，并添加在某个位置.
        var _ = this;
        var a = [];
        p.Webs.forEach(function (_web) { //把子元素的id收集起来，因为要防止ID冲突
            a.push("{'ID':'" + _web.ID + "'}");
        });
        var tmpRowData;
        if (p.Webs.length == 0) {
            b = "'row':{'ID':'" + p.ID + "'}";
        } else {
            b = "'row':{'ID':'" + p.ID + "','web':[" + a.join(',') + "]}";
        }
        var t = "{'action':'CopyRow','data':{'from': {'table':{'ID':'" + _.Parent.ID + "','row':{'ID':'" + _.ID + "'}}},'to':{'table':{'ID':'" + p.Parent.ID + "'," + b + "}},'index':'" + q + "'}}";
        g.ComWithServer.add(g.domainDirectory + "/ResAddApp.aspx?paraStr=" + paraStr +"&flag=" + g.subdir, t);
        g.ComWithServer.startSend(); //将信息发往服务器.
        g.tip.display(lan["02"]);
    };

    _.f = function () {
        var _ = this;
        var t = "{'action':'DeleteRow','data':{'table':{'ID':'" + _.Parent.ID + "','row':{'ID':'" + _.ID + "'}}}";
        g.ComWithServer.add(g.domainDirectory + "/ResAddApp.aspx?paraStr=" + paraStr +"&flag=" + g.subdir, t);
        g.ComWithServer.startSend(); //将信息发往服务器.
        g.tip.display(lan["02"]);
    };

    _.u = function () {
        var _ = this;
        var t = "{'action':'UpdateRow','data':{'table':{'ID':'" + _.Parent.ID + "','row':{'ID':'" + _.ID + "','CateName':'" + _.CateName + "'}}}";
        g.ComWithServer.add(g.domainDirectory + "/ResAddApp.aspx?paraStr=" + paraStr +"&flag=" + g.subdir, t);
        g.ComWithServer.startSend(); //将信息发往服务器.
        g.tip.display(lan["02"]);
    };

    _.v = function (_table, _index) { //移动当前row到_table的_index这个位置.
        var _ = this;
        var t = "{'action':'MoveRow','data':{'from': {'table':{'ID':'" + _.Parent.ID + "','row':{'ID':'" + _.ID + "'}}},'to':{'table':{'ID':'" + _table.ID + "'}},'index':'" + _index + "'}}";
        g.ComWithServer.add(g.domainDirectory + "/ResAddApp.aspx?paraStr=" + paraStr +"&flag=" + g.subdir, t);
        g.ComWithServer.startSend(); //将信息发往服务器.
        g.tip.display(lan["02"]);
    };

    _.moveRowItem = function (_table, _index) { //移动当前row到_table的_index这个位置.
        var _ = this;
        _.v(_table, _index); //发送到服务器.
        for (var i = 0; i < _.Parent.Categories.length; i++) {
            if (_.Parent.Categories[i].ID == _.ID) {
                if ((_index > i) && (_.Parent.ID == _table.ID)) { //如果向下移动并且在同一table中，那么删除原来的webRow的位置后,_index应该减1
                    _index--;
                }
                _.Parent.Categories.splice(i, 1);

                break;
            }
        }
        _table.Categories.splice(_index, 0, _);
        _.Parent.Parent.d(); //当row跨两个table时，要更新两个table的坐标信息。
        _.Parent.Parent.refresh();
        _.Parent = _table;
        _.Parent.Parent.d();
        _.Parent.Parent.refresh();
    };

    /***
     * @param {Web, Number} _paraWeb为要添加的web, _index为要插入的位置.
     */
    _.o = function (p, q) { //tmp:a,b   =addWebItem(_Web,_Number);
        var _ = this;
        _.Webs.splice(q, 0, p);
        p.Parent = _; //将新添加的web的Parent改为此row
        _.lastOperateWeb = p;
        _.Parent.lastOperateRow = _;

        p.a(q); //保存到服务器
        if (lib.data.getOrder(_.Webs, p) > lib.data.getOrder(_.Webs, _.m())) { //如果刚添加的web在此行后显示,则把它调到这行中.
            var a = lib.data.getOrder(_.Webs, p);
            var b = 0;
            for (var i = a; i >= 0; i--) {
                b = b + lib.data.getStrLength(_.Webs[i].Name) * 8 + 10; //从后往前累加字符串长度.
                if (b > g.mainTableWidth) {
                    _.numDisFirst = i + 1; //在最后添加，当空间不足时，把游标向右移动一格
                    break;
                }
            }
        }

        var t = "{'action':'numDisFirst','data':{'table':{'ID':'" + _.Parent.ID + "','row':{'ID':'" + _.ID + "','numDisFirst':'" + _.numDisFirst + "'}}}}";
        g.ComWithServer.add(g.domainDirectory + "/ResAddApp.aspx?paraStr=" + paraStr +"&flag=" + g.subdir, t);
        g.ComWithServer.startSend(); //将信息发往服务器.
        _.Parent.Parent.d();
        _.Parent.Parent.refresh(); //要先刷新，后获取元素位置、大小.
    };

    _.g = function (p) {
        var _ = this;
        for (var i = 0; i < _.Webs.length; i++) {
            if (_.Webs[i] === p) {
                p.f();
                _.Webs.splice(i, 1);
                _.d();
                return;
            }
        }
    };

    _.h = function () {
        var _ = this;
        if (!u(_.lastOperateWeb)) {
            _.lastOperateWeb.g();
        }
    };

    _.n = function () { //添加web时，显示编辑框.===========================
        var _ = this;
        $SA(_.editAndAddEle, "relationalID", _.Parent.ID + "-" + _.ID);
        lib.dom.clearChildren(_.editAndAddEle); //clear all the children of "_.editAndAddEle", or else the children would increase.
        lib.dom.setStyle(_.editAndAddEle, "width:212px;font-size:14px;color:black;position:absolute;border: 1px solid #629444;");
        _.editAndAddEle.id = "editAndAddEleID" + _.ID;
        jQuery(_.editAndAddEle).addClass('editAndAddEle')
        if (Sys.isMiniScreen) { //如果屏幕过小
            var dl = $CE("DL");
            dl.innerHTML += "<span class='wsd' style='cursor:default;float:right;' relationalID='" + _.Parent.ID + "-" + _.ID + "' onclick='webClose(this)'></span>";
            var dd = $CE("DD");
            var a = document.createTextNode(lan["title"]);
            var b = $CE("INPUT");
            $SA(b, "id", "name_editAndAddWeb" + _.ID);
            $SA(b, "relationalID", _.Parent.ID + "-" + _.ID);
            b.onfocus = function () {
                var t = $("name_editAndAddWeb" + _.ID);
                if (t.value == lan["29"]) {
                    t.value = ""; //清空内容
                }
                resumeStyle($("name_editAndAddWeb" + _.ID)); //css恢复原状.
            };
            $SA(b, "onkeydown", "addWebAndClose(this)");
            b.type = "text";
            dd.appendChild(a);
            dd.appendChild(b);
            dl.appendChild(dd);

            var dd = $CE("DD");
            var a = document.createTextNode(lan["link"]);
            var b = $CE("INPUT");
            $SA(b, "id", "link_editAndAddWeb" + _.ID);
            $SA(b, "relationalID", _.Parent.ID + "-" + _.ID);
            b.onfocus = function () {
                resumeStyle($("link_editAndAddWeb" + _.ID));
            };
            $SA(b, "onkeydown", "addWebAndClose(this)");
            b.type = "text";
            dd.appendChild(a);
            dd.appendChild(b);
            dl.appendChild(dd);

            var dd = $CE("DD");
            var a = document.createTextNode(lan["note"]);
            var b = $CE("INPUT");
            $SA(b, "id", "note_editAndAddWeb" + _.ID)
            $SA(b, "relationalID", _.Parent.ID + "-" + _.ID);
            b.onfocus = function () {
                resumeStyle($("note_editAndAddWeb" + _.ID));
            };
            $SA(b, "onkeydown", "addWebAndClose(this)");
            b.type = "text";
            dd.appendChild(a);
            dd.appendChild(b);
            dl.appendChild(dd);

            dl.innerHTML += "<dd><input type='button' relationalID='" + _.Parent.ID + "-" + _.ID + "' value='" + lan["save"] + "' onclick='addWebAndClose(this)'/></dd>";
            _.editAndAddEle.appendChild(dl);
            _.Parent.Parent.E.appendChild(_.editAndAddEle);
            lib.dom.disAtCenter(_.editAndAddEle);
        } else {
            function stringDom(dom) {
                var o = document.createElement("div");
                o.appendChild(dom);
                return o.innerHTML
            }
            var a = document.createTextNode(lan["title"]); // 标题
            a = "<div class='label layertitle'>" + stringDom(a) + "</div>"
            var b = $CE("INPUT");
            $SA(b, "id", "name_editAndAddWeb" + _.ID);
            $SA(b, "relationalID", _.Parent.ID + "-" + _.ID);
            jQuery(b).addClass('input_editor_modify');
            b.onfocus = function () {
                var t = $("name_editAndAddWeb" + _.ID);
                if (t.value == lan["29"]) {
                    t.value = ""; //清空内容
                }
                resumeStyle(t); //css恢复原状.
            };
            $SA(b, "onkeydown", "addWebAndClose(this)");
            b.type = "text";

            jQuery(_.editAndAddEle).append(a)
            jQuery(_.editAndAddEle).append(b)
            // _.editAndAddEle.appendChild(a);
            // _.editAndAddEle.appendChild(b);

            var a = document.createTextNode(lan["link"]); // 链接
            a = "<div class='label'>" + stringDom(a) + "</div>"
            var b = $CE("INPUT");
            $SA(b, "id", "link_editAndAddWeb" + _.ID);
            $SA(b, "relationalID", _.Parent.ID + "-" + _.ID);
            jQuery(b).addClass('input_editor_modify');
            b.onfocus = function () {
                resumeStyle($("link_editAndAddWeb" + _.ID));
            };
            $SA(b, "onkeydown", "addWebAndClose(this)");
            b.type = "text";
            // _.editAndAddEle.appendChild(a);
            // _.editAndAddEle.appendChild(b);
            jQuery(_.editAndAddEle).append(a)
            jQuery(_.editAndAddEle).append(b)

            var a = document.createTextNode(lan["note"]); // 备注
            a = "<div class='label'>" + stringDom(a) + "</div>"
            var b = $CE("INPUT");
            $SA(b, "id", "note_editAndAddWeb" + _.ID)
            $SA(b, "relationalID", _.Parent.ID + "-" + _.ID);
            jQuery(b).addClass('input_editor_modify');
            b.onfocus = function () {
                resumeStyle($("note_editAndAddWeb" + _.ID));
            };
            $SA(b, "onkeydown", "addWebAndClose(this)");
            b.type = "text";
            // _.editAndAddEle.appendChild(a);
            // _.editAndAddEle.appendChild(b);
            jQuery(_.editAndAddEle).append(a)
            jQuery(_.editAndAddEle).append(b)
            _.editAndAddEle.innerHTML += "<input type='button' relationalID='" + _.Parent.ID + "-" + _.ID + "' value='" + lan["save"] + "' onclick='addWebAndClose(this)'/>";
            _.editAndAddEle.innerHTML = "<span class='wsd' style='cursor:pointer;position: absolute; right:12px;top:6px;font-size:24px;' relationalID='" + _.Parent.ID + "-" + _.ID + "' onclick='webClose(this)'></span>" + _.editAndAddEle.innerHTML;
            _.Parent.Parent.E.appendChild(_.editAndAddEle);
            _.editAndAddEle.style.left = '50%';
            _.editAndAddEle.style.transform = 'translateX(-50%)';
            _.editAndAddEle.style.top = (_.RecTop - _.editAndAddEle.offsetHeight) + "px";
        }
    };

    _.$ = function (p) {
        var _ = this;
        for (var i = 0; i < _.Webs.length; i++) {
            if (p == _.Webs[i].ID) {
                return _.Webs[i];
            }
        }
    }

    _.m = function () {
        var _ = this;
        var t = 0;
        for (var i = _.numDisFirst; i < _.Webs.length; i++) {
            var tmpWeb = _.Webs[i];
            t = t + lib.data.getStrLength(tmpWeb.Name) * 8 + 10;
            if (t > g.mainTableWidth) { //显示10个lin
                return _.Webs[i - 1];
            }
        }
        return _.Webs[_.Webs.length - 1];
    };


    _.j = function (p) { //tmp:t,
        var _ = this;
        var t = {};
        if (p) {
            t.Id = lib.Time.getID() + "CATE";
        } else {
            t.Id = _.ID;
        }
        t.Ct = _.CateName;
        t.NumDisFirst = _.NumDisFirst;
        t.Mrowwebs = {};
        t.Mrowwebs.web = [];
        _.Webs.forEach(function (_paraWeb) {
            t.Mrowwebs.web.push(_paraWeb.j(_isNewID));
        });
        return t;
    };

    _.d = function () { //tmp:t,a,b,    to display this WebRow.
        var _ = this;
        //lib.dom.setStyle(_.E, "display:default;");
        //_.E.parentNode = _.Parent.E;
        var tmp = _.c();
        //_.E = tmp;  //for test.
    };
};

B.WebTableBase = function () {
    var _ = this;

    _.l = function () { //tmp:t,
        var _ = this;
        var t = new WebTable((new Date).getTime() + "TB", _.Name, _.Container, _.Parent);
        for (var i = 0; i < _.Categories.length; i++) {
            t.Categories.push(_.Categories[i].l());
        }
        return t;
    };

    _.b = function (p, q) { //tmp:t,a,b,c
        var _ = this;
        var a = [];
        var t = "";
        p.Categories.forEach(function (_row) { //把子元素的id收集起来，以防止ID冲突
            var c = [];
            _row.Webs.forEach(function (_web) {
                c.push("{'ID':'" + _web.ID + "'}");
            });
            t = "{'ID':'" + _row.ID + "','web':[" + c.join(",") + "]}";
            a.push(t);
        });
        var t = "{'action':'CopyTable','data':{'from':{'table':{'ID':'" + _.ID + "'}},'to':{'table':{'ID':'" + p.ID + "','row':[" + a.join(",") + "]}},'index':'" + q + "'}}";
        g.ComWithServer.add(g.domainDirectory + "/ResAddApp.aspx?paraStr=" + paraStr +"&flag=" + g.subdir, t);
        g.ComWithServer.startSend(); //将信息发往服务器.
        g.tip.display(lan["02"]);
    };
    _.a = function (p) { //添加一个table
        var _ = this;
        var t = "{'action':'AddTable','data':{'table':{'ID':'" + _.ID + "','Name':'" + _.Name + "'},'index':'" + p + "'}}";
        g.ComWithServer.add(g.domainDirectory + "/ResAddApp.aspx?paraStr=" + paraStr +"&flag=" + g.subdir, t);
        g.ComWithServer.startSend(); //将信息发往服务器.
        g.tip.display(lan["02"]);
    };
    _.v = function (_page, _index) { //移动table
        var _ = this;
        var t = "{'action':'MoveTable','data':{'from':{'table':{'ID':'" + _.ID + "'}},'index':'" + _index + "'}}";
        g.ComWithServer.add(g.domainDirectory + "/ResAddApp.aspx?paraStr=" + paraStr +"&flag=" + g.subdir, t);
        g.ComWithServer.startSend(); //将信息发往服务器.
        g.tip.display(lan["02"]);
    };
    _.f = function () { //添加一个table
        var _ = this;
        var t = "{'action':'DeleteTable','data':{'table':{'ID':'" + _.ID + "'}}}";
        g.ComWithServer.add(g.domainDirectory + "/ResAddApp.aspx?paraStr=" + paraStr +"&flag=" + g.subdir, t);
        g.ComWithServer.startSend(); //将信息发往服务器.
        g.tip.display(lan["02"]);
    };

    _.u = function () {
        var _ = this;
        var t = "{'action':'UpdateTable','data':{'table':{'ID':'" + _.ID + "','Name':'" + _.Name + "'}}}";
        g.ComWithServer.add(g.domainDirectory + "/ResAddApp.aspx?paraStr=" + paraStr +"&flag=" + g.subdir, t);
        g.ComWithServer.startSend(); //将信息发往服务器.
        g.tip.display(lan["02"]);
    };

    _.moveTableItem = function (_page, _index) {
        var _ = this;
        _.v(_page, _index); //发送到服务器.
        for (var i = 0; i < _.Parent.Tables.length; i++) {
            if (_.Parent.Tables[i].ID == _.ID) {
                if ((_index > i) && (_.Parent.ID == _page.ID)) { //如果向下移动并且在同一page中，那么删除原来的webRow的位置后,_index应该减1
                    _index--;
                }
                _.Parent.Tables.splice(i, 1);
                _.Parent.d();
                _.Parent.refresh();
                break;
            }
        }
        _page.Tables.splice(_index, 0, _);
        _.Parent = _page;
        _page.d();
        _page.refresh();
    };

    _.j = function (p) {
        var _ = this;
        var tmpData = {};
        tmpData.page = {};
        if (p) {
            tmpData.page.Id = lib.Time.getID() + "TABLE";
        } else {
            tmpData.page.Id = _.ID;
        }
        tmpData.page.Mrows = {};
        tmpData.page.Mrows.row = [];
        _.Categories.forEach(function (_paraCate) {
            tmpData.page.Mrows.row.push(_paraCate.j(p));
        });
        return tmpData;
    };

    _.d = function () {
        var _ = this;
        var tmp = _.Container;
        if (!u(tmp)) {
            var index = lib.data.getOrder(_.Parent.Tables, _);
            _.Container.removeChild(_.E);
            var ele = _.c().E;
            if (u(_.Container.children) || index == _.Container.children.length) { //如果没有元素或是最后的元素.
                _.Container.appendChild(ele);
            } else {
                _.Container.insertBefore(ele, _.Container.children[index]);
            }
        }
        return _;
    };

    _.$ = function (p) {
        var _ = this;
        for (var i = 0; i < _.Categories.length; i++) {
            if (p == _.Categories[i].ID) {
                return _.Categories[i];
            }
        }
    };
};

B.PageBase = function () {
    var _ = this;
    _.getTablesBelow = function (p_x) {
        var _ = this,
            tmp = [];
        for (var i = 0; i < _.Tables.length; i++) {
            if (x > _.Tables[i].RecBottom) {
                tmp.push(_.Tables[i]);
            }
        }
    };
};
var bB = new B.WebBase();
var cB = new B.WebRowBase();
var dB = new B.WebTableBase();
var eB = new B.PageBase();
//----------------start---------------
if (U != g.page) {
    g.page.getTablesBelow = eB.getTablesBelow; //将属性传给全局变量g.page。
}
//----------------end-----------------
g.page.Tables?.forEach(function (_table) {
    _table.l = dB.l;
    _table.b = dB.b;
    _table.a = dB.a;
    _table.v = dB.v;
    _table.f = dB.f;
    _table.u = dB.u;
    _table.moveTableItem = dB.moveTableItem;
    _table.j = dB.j;
    _table.d = dB.d;
    _table.$ = dB.$;

    _table.Categories.forEach(function (_row) {
        _row.getFWebFromPreRow = cB.getFWebFromPreRow;
        _row.getFWebFromNexRow = cB.getFWebFromNexRow;
        _row.closeEditor = cB.closeEditor;
        _row.l = cB.l;
        _row.a = cB.a;
        _row.b = cB.b;
        _row.f = cB.f;
        _row.u = cB.u;
        _row.v = cB.v;
        _row.moveRowItem = cB.moveRowItem;
        _row.o = cB.o;
        _row.g = cB.g;
        _row.h = cB.h;
        _row.n = cB.n;
        _row.$ = cB.$;
        _row.m = cB.m;
        _row.j = cB.j;
        _row.d = cB.d;

        _row.Webs.forEach(function (_web) {
            _web.l = bB.l;
            _web.a = bB.a;
            _web.b = bB.b;
            _web.f = bB.f;
            _web.u = bB.u;
            _web.v = bB.v;
            _web.g = bB.g;
            _web.moveWebItem = bB.moveWebItem;
            _web.h = bB.h;
            _web.i = bB.i;
            _web.j = bB.j;
            _web.k = bB.k;
            _web.m = bB.m;
            _web.n = bB.n;
        });
    });
});

g.sendAppToUser = function (_app, _user) { //将应用模块发给某用户.
    var data = "{'action':'CopyApp','from':'" + g.DomainSuf + "/" + g.subdir + "','to':'','app':" + _app + "}";
    g.ComWithServer.add(g.domainDirectory + "/ResAddApp.aspx?paraStr=" + paraStr +"&flag=" + g.subdir, data);
    g.ComWithServer.startSend(); //将信息发往服务器.
    g.tip.display("分享数据...", 2);
};

g.addWebToUser = function () {
    var e = lib.formatEvent();
    var t = e.target;
    var tmpIDArray = t.getAttribute("relationalID").split("-");
    var tmpTable = g.page.$(tmpIDArray[0]);
    var tmpRow = tmpTable.$(tmpIDArray[1]);
    var tmpWeb = tmpRow.$(tmpIDArray[2]);
    var data = "{'data':{'table':{'ID':'" + a.Parent.ID + "','row':{'ID':'" + tmpRow.ID + "','web':{'ID':'" + tmpWeb.ID + "'}}},'Type':'000000'}";
    g.sendAppToUser(data, g.user.username);
};

g.addRowToUser = function () { //在没有用户和主人不一样的情况下，用户触发了添加一个WebRow人事件，这是它要调用的函数.
    var e = lib.formatEvent();
    var t = e.target;
    var tmpIDArray = t.getAttribute("relationalID").split("-");
    var tmpTable = g.page.$(tmpIDArray[0]);
    var a = tmpTable.$(tmpIDArray[1]);
    var data = "{'data':{'table':{'ID':'" + a.Parent.ID + "','row':{'ID':'" + a.ID + "'}},'Type':'000001'}";
    g.sendAppToUser(data, g.user.username);
};

g.addTableToUser = function () {
    var e = lib.formatEvent();
    var t = e.target;
    var tmpIDArray = t.getAttribute("relationalID").split("-");
    var tmpTable = g.page.$(tmpIDArray[0]);
    var data = "{'data':{'table':{'ID':'" + tmpTable.ID + "'},'Type':'000002'}";
    g.sendAppToUser(data, g.user.username);
};


//+----------------a class for communicating with the server-------------------
g.ComWithServer = new ComWithServer();

function ComWithServer() {
    this.dataToBeSent = [];
    this.dataHasBeenSent = [];
    this.dataToBeSentLen = 0;
    this.dataHasBeenSentLen = 0;
    this.totalLen = 0; //  totalLen = dataToBeSentLen + dataHasBeenSentLen
    this.status = 0;


    function DataSent(index, indexInTOH, url, data) {
        this.index = index;
        this.indexInTOH = indexInTOH; //the index of the item in "dataToBeSent" array or in "dataHasBeenSent".
        this.url = url;
        this.data = data;
        this.hasSent = false;
    }

    this.add = function (url, data) {

        var oneDataSent = new DataSent(g.ComWithServer.totalLen, g.ComWithServer.dataToBeSentLen, url, data); //instance
        this.dataToBeSent.push(oneDataSent); // "totalLen" is the index of  this item of data.

    };

    this.sendReq = function (t) {
        lib.ajax({
            type: "POST",
            cache: false,
            url: t.url,
            dataType: "json",
            contentType: "application/x-www-form-urlencoded",
            data: t.data,
            async: true,
            success: function (res) {

                if (res.toLowerCase().indexOf("successed") > -1) {
                    g.ComWithServer.dataToBeSent.splice(0, 1)

                    g.ComWithServer.dataHasBeenSent.push(t);
                    g.ComWithServer.dataHasBeenSentLen = g.ComWithServer.dataHasBeenSentLen + 1;
                    if (g.ComWithServer.dataToBeSent.length > 0) {
                        g.ComWithServer.sendReq(g.ComWithServer.dataToBeSent[0]);
                    }
                }
            },
            error: function (msg) {
                for (var info in msg)
                    console.log(info + " = " + msg[info]);
                g.tip.display(lan["32"]);
            }
        });
    };

    this.startSend = function () { //working......  如果用数据主人没有登录，需要缓存数据。
        if (g.ComWithServer.status == 0) {
            g.ComWithServer.sendReq(g.ComWithServer.dataToBeSent[0]);
        }
    };
}

g.selectedData = g.selectedData || {};
g.selectedData.m = function () {
    if (!u(this.tableData) && !u(this.rowData) && !u(this.webData)) {
        this.webData.E.style.backgroundColor = "#629444"; // 操作后， 颜色加深
    }
};

g.selectedData.resume = function () {
    if (!u(this.webData)) {
        this.webData.E.style.backgroundColor = "#DDEEDD";
    }
};

g.craw = function (p) {
    var data;
    if (p == 0) {
        var data = $("websiteID").value;
        if (!(data.match(/(http|https|ftp|mms|rtsp|gopher|socks4|socks5|thunder|flashget|qqdl):\/\/.*/))) {
            data = "http://" + data;
        }
    } else if (p == 1) {
        data = $("webdataID").value;
    } else { }
    var url = g.domainDirectory + "/crawler.aspx?f=" + (new Date).getTime();

    postAndGetData(true, url, data, function (p) {
        if (p == "1") { //1 means successed
            sendAndGetData(false, getDataURL, function (resTxt) { //刷新数据.
                g.dataWebTable["main"] = eval(resTxt);
            });
            g.page.bindData(g.dataWebTable["main"]);
            g.page.createDom();
            g.page.refresh();
        }
    });

    closeWindow(lib.formatEvent().target.parentNode.parentNode.parentNode);
}


g.getHostDatafunction = function () //获取网页主人的数据
{

    g.subdir = "host";
    var getDataURL = g.domainDirectory + "/SendDataToClient.aspx?owner=1&&flag=host&&freshParam=" + (new Date).getTime();
    sendAndGetData(true, getDataURL, function (resTxt) {
        g.dataWebTable["main"] = eval('(' + resTxt + ')');
        g.page.bindData(g.dataWebTable["main"]);
        g.page.createDom();
        g.page.refresh();
    });
};

//+---------------add a row to the table--------------
g.editAddCategory = function (value) {
    var editAddRowEle = $CE("DIV");
    $SA(editAddRowEle, "relationalID", value.getAttribute("relationalID"));
    editAddRowEle.style.position = "absolute";
    editAddRowEle.style.backgroundColor = "#fff";
    editAddRowEle.style.border = "1px solid #629444";
    editAddRowEle.style.padding = "5px";
    editAddRowEle.style.fontSize = "14px";
    editAddRowEle.innerHTML = lan["category"] + "<input type='text' style='margin: 0 5px;' onfocus='resumeStyle(this)' onkeydown='addCategory(this)'/><input class='input_addCategory' style='background: #629444;color: #fff;border: 0;font-size: 12px;font-weight: 400;line-height: 20px;' type='button' value='" + lan["save"] + "' onclick='addCategory(this)'/><span style='cursor:default;margin-left: 10px;' onclick='closeWindow(this.parentNode);'>&nbsp;X&nbsp;</span>";
    addEleAround(value, editAddRowEle, "rightbottom");
};

g.createWebTable = function (_paraData) { //将数据导入表中，并显示.
    g.page.bindData(_paraData);
    g.page.createDom();
    g.page.refresh();
};

g.triggerSearchLocal = function (e) {
    var tmpEve = lib.formatEvent(e);
    if (tmpEve.keyCode == 13) {
        g.searchLocal();
    }
};

g.searchUserData = function (key) {
    key = key.replace(/(^\s*)|(\s*$)/g, "").toLowerCase();
    var pageData = eval('(' + g.lastPageDataString + ')');
    if ('' !== key) {
        for (var i = 0, tables = pageData.page.Mtables.table, lenI = tables.length; i < lenI; i++) {
            var table = tables[i];
            var isDisplayTable = false;
            if (table.Mrows && '[object Array]' === toString.call(table.Mrows.row)) {
                for (var j = 0, rows = table.Mrows.row, lenJ = rows.length; j < lenJ; j++) {
                    var row = rows[j];
                    var isDisplayRow = false;
                    if (row.Ct.indexOf(key) > -1) {
                        isDisplayTable = true;
                        continue;
                    }
                    if (row.Mrowwebs && '[object Array]' === toString.call(row.Mrowwebs.web)) {
                        for (var k = 0, webs = row.Mrowwebs.web, lenK = webs.length; k < lenK; k++) {
                            var web = webs[k];
                            if ((web.Name || '').indexOf(key) > -1 || (web.Note || '').indexOf(key) > -1) {
                                isDisplayTable = isDisplayRow = true;
                            } else {
                                webs.splice(k, 1);
                                --k;
                                --lenK;
                            }
                        }
                    }
                    if (false === isDisplayRow) {
                        rows.splice(j, 1);
                        --j;
                        --lenJ;
                    }
                }
                if (false === isDisplayTable) {
                    tables.splice(i, 1);
                    --i;
                    --lenI;
                }
            }
        }
    }
    return pageData;
};


g.searchLocal = function () {
    var tmpKey = $("webKeywordID").value;
    var tmpUrl;
    if (tmpKey.replace(/^\s*/, "") == "") { //如果输入关键字为空，则把最初的数据重新导入表中.
        g.createWebTable(g.dataWebTable["main"]);
        return;
    }
    g.dataWebTable["search" + tmpKey] = jQuery.extend(true, {},
        g.dataWebTable["main"]);

    if (g.dataWebTable["search" + tmpKey].page.Mtables.table && !g.dataWebTable["search" + tmpKey].page.Mtables.table.length) {
        var tmp = g.dataWebTable["search" + tmpKey].page.Mtables.table;
        g.dataWebTable["search" + tmpKey].page.Mtables.table = [tmp];
    }
    for (var i = 0; i < g.dataWebTable["search" + tmpKey].page.Mtables.table.length; i++) {

        var table = g.dataWebTable["search" + tmpKey].page.Mtables.table[i];
        var isKeepTable = lib.data.isLike(tmpKey, table.Name);
        if (isKeepTable) {
            continue; // 保留此table
        }

        if (table.Mrows && table.Mrows.row && !table.Mrows.row.length) {
            var tmp = table.Mrows.row;
            table.Mrows.row = [tmp];
        }
        for (var j = 0; !!table.Mrows && j < table.Mrows.row.length; j++) {
            var isKeepRow = false;
            var row = table.Mrows.row[j];

            if (lib.data.isLike(tmpKey, row.Ct)) {
                isKeepRow = true;
                isKeepTable = true;
                continue;
            }

            if (row.Mrowwebs && row.Mrowwebs.web && !row.Mrowwebs.web.length) {
                var tmp = row.Mrowwebs.web;
                row.Mrowwebs.web = [tmp];
            }
            for (var k = 0; !!row.Mrowwebs && k < row.Mrowwebs.web.length; k++) {
                var isKeepWeb = false;
                var web = row.Mrowwebs.web[k];

                if (lib.data.isLike(tmpKey, web.Name) || lib.data.isLike(tmpKey, web.Link) || lib.data.isLike(tmpKey, web.Note)) {
                    isKeepTable = true;
                    isKeepRow = true;
                    isKeepWeb = true;
                }

                if (false == isKeepWeb) {
                    row.Mrowwebs.web.splice(k, 1);
                    k--;
                }
            }

            if (false == isKeepRow) {
                table.Mrows.row.splice(j, 1);
                j--;
            }
        }

        if (false == isKeepTable) {
            g.dataWebTable["search" + tmpKey].page.Mtables.table.splice(i, 1);
            i--;
        }
    }

    // if (u(g.dataWebTable["search" + tmpKey])) {
    // tmpUrl = g.domainDirectory + "/search.aspx?key=" + escape(tmpKey) + "&&f=" + (new Date).getTime();
    // sendAndGetData(true, tmpUrl, function (_resData) {
    // if (_resData == "") {
    // }
    // else {
    // g.dataWebTable["search" + tmpKey] = eval('(' + _resData + ')'); //如果次数过多，会不会占用很多内存，以后要测试.
    // }
    // });
    // }
    // // 合并私人的tables和数据库中tables
    // var mergence = [];
    // if (undefined !== g.dataWebTable["search" + tmpKey]) {
    // var tmpTables = g.dataWebTable["search" + tmpKey].page.Mtables.table;
    // for (var i = 0, len = tmpTables.length; i < len; i++) {
    // mergence.push(tmpTables[i]);
    // }
    // }
    // var tmpPrivateSearchTables = g.searchUserData(tmpKey).page.Mtables.table;
    // for (var i = 0, len = tmpPrivateSearchTables.length; i < len; i++) {
    // mergence.push(tmpPrivateSearchTables[i]);
    // }
    // if (undefined === g.dataWebTable["search" + tmpKey]) {
    // g.dataWebTable["search" + tmpKey] = { page: { Mtables: { table: [mergence]}} };
    // } else {
    // g.dataWebTable["search" + tmpKey].page.Mtables.table = mergence;
    // }
    g.createWebTable(g.dataWebTable["search" + tmpKey]);
    g.state["search"] = true;
};

g.goUserPage = function () { //转到用户的个人页面.
    window.location.href = "http:\/\/" + g.user.username + "." + g.mainDomain; //中转到新的url
};